
import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async(to, from, next) => {
  if (store.state.user.token) {
  //  说明已登录
    if (to.path === '/login') {
      next('/')
    } else {
      // 判断vuex是否有用户信息,没有就获取
      if (!store.state.user.UserInfo) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    // 没有登录
    // 没有token 说明未登录
    // 设置白名单 运行不需要token的页面直接放行
    const white = ['/login', '/404']
    if (white.includes(to.path)) {
      // true 允许放行
      next()
    } else {
      // false 跳转到登录页
      next('/login')
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
